<!DOCTYPE html>
<html prefix="og: http://ogp.me/ns# article: http://ogp.me/ns/article#
" lang="zh_cn">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>使用C# Interactive交互式运行C#代码 | vitamind3 竹里馆</title>
<link href="../../assets/css/all-nocdn.css" rel="stylesheet" type="text/css">
<link href="../../assets/css/ipython.min.css" rel="stylesheet" type="text/css">
<link href="../../assets/css/nikola_ipython.css" rel="stylesheet" type="text/css">
<meta name="theme-color" content="#5670d4">
<meta name="generator" content="Nikola (getnikola.com)">
<link rel="alternate" type="application/rss+xml" title="RSS" hreflang="zh_cn" href="../../rss.xml">
<link rel="canonical" href="https://vitamind3.gitee.io/posts/shi-yong-c-interactivejiao-hu-shi-yun-xing-cdai-ma-shi-li/">
<!--[if lt IE 9]><script src="../../assets/js/html5.js"></script><![endif]--><meta name="author" content="vitamind3">
<link rel="prev" href="../tiao-zhan-1xiao-shi-pythonbian-ma-ti-qu-he-suan-bao-gao-shu-ju/" title="挑战1小时python编码提取核酸报告数据" type="text/html">
<link rel="next" href="../tui-jian-wang-pan-tong-bu-pan-shu-ju-jia-mi-gong-ju-restic/" title="推荐网盘同步盘数据加密工具restic" type="text/html">
<meta property="og:site_name" content="vitamind3 竹里馆">
<meta property="og:title" content="使用C# Interactive交互式运行C#代码">
<meta property="og:url" content="https://vitamind3.gitee.io/posts/shi-yong-c-interactivejiao-hu-shi-yun-xing-cdai-ma-shi-li/">
<meta property="og:description" content="使用C# Interactive交互式运行C#代码¶.NET Interactive Notebooks¶一些脚本语言例如Python可以在控制台进行REPL交互操作，能快速的执行一些命令。以前执行C#每次都最需要创建一个新项目，略显烦琐。
但现在可以通过 Visual Studio Code 的 .NET Interactive Notebooks 插件方便的在VSCode 中交互式的使用C#代">
<meta property="og:type" content="article">
<meta property="article:published_time" content="2022-04-19T11:08:30+08:00">
<meta property="article:tag" content=".net">
<meta property="article:tag" content="aspnet">
<meta property="article:tag" content="C#">
</head>
<body>
<a href="#content" class="sr-only sr-only-focusable">跳到主内容</a>

<!-- Menubar -->

<nav class="navbar navbar-expand-md static-top mb-4
navbar-dark
bg-dark
"><div class="container">
<!-- This keeps the margins nice -->
        <a class="navbar-brand" href="../../">

            <span id="blog-title">vitamind3 竹里馆</span>
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#bs-navbar" aria-controls="bs-navbar" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="bs-navbar">
            <ul class="navbar-nav mr-auto">
<li class="nav-item">
<a href="../../pages/introduction" class="nav-link">欢迎</a>
                </li>
<li class="nav-item">
<a href="../../archive.html" class="nav-link">文章归档</a>
                </li>
<li class="nav-item">
<a href="../../categories/" class="nav-link">分类</a>
                </li>
<li class="nav-item">
<a href="../../rss.xml" class="nav-link">RSS 源</a>

                
            </li>
</ul>
<ul class="navbar-nav navbar-right"></ul>
</div>
<!-- /.navbar-collapse -->
    </div>
<!-- /.container -->
</nav><!-- End of Menubar --><div class="container" id="content" role="main">
    <div class="body-content">
        <!--Body content-->
        
        
        
<article class="post-text h-entry hentry postpage" itemscope="itemscope" itemtype="http://schema.org/Article"><header><h1 class="p-name entry-title" itemprop="headline name"><a href="." class="u-url">使用C# Interactive交互式运行C#代码</a></h1>

        <div class="metadata">
            <p class="byline author vcard p-author h-card"><span class="byline-name fn p-name" itemprop="author">
                    vitamind3
            </span></p>
            <p class="dateline">
            <a href="." rel="bookmark">
            <time class="published dt-published" datetime="2022-04-19T11:08:30+08:00" itemprop="datePublished" title="2022-04-19 11:08">2022-04-19 11:08</time></a>
            </p>
            

        </div>
        

    </header><div class="e-content entry-content" itemprop="articleBody text">
    <div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="%E4%BD%BF%E7%94%A8C%23-Interactive%E4%BA%A4%E4%BA%92%E5%BC%8F%E8%BF%90%E8%A1%8CC%23%E4%BB%A3%E7%A0%81">使用C# Interactive交互式运行C#代码<a class="anchor-link" href="#%E4%BD%BF%E7%94%A8C%23-Interactive%E4%BA%A4%E4%BA%92%E5%BC%8F%E8%BF%90%E8%A1%8CC%23%E4%BB%A3%E7%A0%81">¶</a>
</h2>
<h3 id=".NET-Interactive-Notebooks">.NET Interactive Notebooks<a class="anchor-link" href="#.NET-Interactive-Notebooks">¶</a>
</h3>
<p>一些脚本语言例如Python可以在控制台进行REPL交互操作，能快速的执行一些命令。以前执行C#每次都最需要创建一个新项目，略显烦琐。
但现在可以通过 Visual Studio Code 的 .NET Interactive Notebooks 插件方便的在VSCode 中交互式的使用C#代码，非常易于快速的测试一些功能。</p>
<!-- TEASER_END -->

<h3 id="%E5%AE%89%E8%A3%85">安装<a class="anchor-link" href="#%E5%AE%89%E8%A3%85">¶</a>
</h3>
<p>需要安装：</p>
<ul>
<li>Visual Studio Code</li>
<li>.Net SDK</li>
<li>.NET Interactive Notebooks</li>
</ul>
<h3 id="%E5%90%AF%E5%8A%A8">启动<a class="anchor-link" href="#%E5%90%AF%E5%8A%A8">¶</a>
</h3>
<p><code>.NET Interactive: Create new blank notebook</code></p>
<h3 id="%E7%A4%BA%E4%BE%8B">示例<a class="anchor-link" href="#%E7%A4%BA%E4%BE%8B">¶</a>
</h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="%E5%9F%BA%E6%9C%AC%E7%9A%84C%23%E6%93%8D%E4%BD%9C">基本的C#操作<a class="anchor-link" href="#%E5%9F%BA%E6%9C%AC%E7%9A%84C%23%E6%93%8D%E4%BD%9C">¶</a>
</h4>
<p>测试 record 类型。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-csharp"><pre><span></span><span class="k">public</span> <span class="n">record</span> <span class="nf">Point</span><span class="p">(</span><span class="kt">double</span> <span class="n">X</span><span class="p">,</span> <span class="kt">double</span> <span class="n">Y</span><span class="p">);</span>
<span class="n">Point</span> <span class="n">pt</span><span class="p">=</span><span class="k">new</span><span class="p">(</span><span class="m">20.1</span><span class="p">,</span><span class="m">30.1</span><span class="p">);</span>
<span class="n">pt</span>
</pre></div>

    </div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

    <div class="prompt"></div>



<div class="output_html rendered_html output_subarea "><table>
<thead><tr>
<th>X</th>
<th>Y</th>
</tr></thead>
<tbody><tr>
<td><div class="dni-plaintext">20.1</div></td>
<td><div class="dni-plaintext">30.1</div></td>
</tr></tbody>
</table></div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="%E5%8A%A0%E8%BD%BDnuget%E5%8C%85">加载nuget包<a class="anchor-link" href="#%E5%8A%A0%E8%BD%BDnuget%E5%8C%85">¶</a>
</h4>
<p>以加载 RestSharp 包为例：</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-csharp"><pre><span></span><span class="err">#</span><span class="n">r</span> <span class="s">"nuget:RestSharp, 107.3.0"</span>
</pre></div>

    </div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

    <div class="prompt"></div>



<div class="output_html rendered_html output_subarea "><div>
<div></div>
<div></div>
<div>
<strong>Installed Packages</strong><ul><li><span>RestSharp, 107.3.0</span></li></ul>
</div>
</div></div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>通过RestSharp包获取天气数据，见 <a href="https://www.sojson.com/blog/305.html">免费天气API，天气JSON API，不限次数获取十五天的天气预报 —技术博客</a> 。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-csharp"><pre><span></span><span class="k">using</span> <span class="nn">RestSharp</span><span class="p">;</span>
<span class="n">RestClient</span> <span class="n">client</span><span class="p">=</span><span class="k">new</span><span class="p">(</span><span class="s">"http://t.weather.itboy.net/api/weather/city"</span><span class="p">);</span>
<span class="n">RestRequest</span> <span class="n">request</span> <span class="p">=</span> <span class="k">new</span><span class="p">(</span><span class="s">"101010100"</span><span class="p">);</span>
<span class="kt">var</span> <span class="n">data</span> <span class="p">=</span> <span class="k">await</span> <span class="n">client</span><span class="p">.</span><span class="n">ExecuteGetAsync</span><span class="p">(</span><span class="n">request</span><span class="p">);</span>
<span class="n">data</span><span class="p">.</span><span class="n">Content</span>
</pre></div>

    </div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

    <div class="prompt"></div>




<div class="output_text output_subarea ">
<pre>{"message":"success感谢又拍云(upyun.com)提供CDN赞助","status":200,"date":"20220419","time":"2022-04-19 10:52:45","cityInfo":{"city":"北京市","citykey":"101010100","parent":"北京","updateTime":"09:01"},"data":{"shidu":"57%","pm25":148.0,"pm10":180.0,"quality":"中度","wendu":"14","ganmao":"儿童、老年人及心脏、呼吸系统疾病患者人群应减少长时间或高强度户外锻炼，一般人群适量减少户外运动","forecast":[{"date":"19","high":"高温 27℃","low":"低温 13℃","ymd":"2022-04-19","week":"星期二","sunrise":"05:30","sunset":"18:56","aqi":135,"fx":"西南风","fl":"2级","type":"霾","notice":"雾霾来袭，戴好口罩再出门"},{"date":"20","high":"高温 29℃","low":"低温 16℃","ymd":"2022-04-20","week":"星期三","sunrise":"05:28","sunset":"18:57","aqi":100,"fx":"南风","fl":"3级","type":"霾","notice":"雾霾来袭，戴好口罩再出门"},{"date":"21","high":"高温 27℃","low":"低温 10℃","ymd":"2022-04-21","week":"星期四","sunrise":"05:27","sunset":"18:58","aqi":35,"fx":"西风","fl":"4级","type":"阴","notice":"不要被阴云遮挡住好心情"},{"date":"22","high":"高温 22℃","low":"低温 10℃","ymd":"2022-04-22","week":"星期五","sunrise":"05:25","sunset":"18:59","aqi":17,"fx":"西南风","fl":"3级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},{"date":"23","high":"高温 27℃","low":"低温 13℃","ymd":"2022-04-23","week":"星期六","sunrise":"05:24","sunset":"19:00","aqi":50,"fx":"南风","fl":"2级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},{"date":"24","high":"高温 29℃","low":"低温 14℃","ymd":"2022-04-24","week":"星期日","sunrise":"05:23","sunset":"19:01","aqi":49,"fx":"东风","fl":"2级","type":"多云","notice":"阴晴之间，谨防紫外线侵扰"},{"date":"25","high":"高温 29℃","low":"低温 13℃","ymd":"2022-04-25","week":"星期一","sunrise":"05:21","sunset":"19:02","aqi":60,"fx":"南风","fl":"2级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},{"date":"26","high":"高温 24℃","low":"低温 13℃","ymd":"2022-04-26","week":"星期二","sunrise":"05:20","sunset":"19:03","aqi":78,"fx":"南风","fl":"3级","type":"晴","notice":"愿你拥有比阳光明媚的心情"},{"date":"27","high":"高温 24℃","low":"低温 13℃","ymd":"2022-04-27","week":"星期三","sunrise":"05:19","sunset":"19:04","aqi":86,"fx":"东南风","fl":"2级","type":"阴","notice":"不要被阴云遮挡住好心情"},{"date":"28","high":"高温 22℃","low":"低温 12℃","ymd":"2022-04-28","week":"星期四","sunrise":"05:17","sunset":"19:05","aqi":55,"fx":"东南风","fl":"2级","type":"多云","notice":"阴晴之间，谨防紫外线侵扰"},{"date":"29","high":"高温 21℃","low":"低温 12℃","ymd":"2022-04-29","week":"星期五","sunrise":"05:16","sunset":"19:06","aqi":72,"fx":"东南风","fl":"2级","type":"多云","notice":"阴晴之间，谨防紫外线侵扰"},{"date":"30","high":"高温 30℃","low":"低温 16℃","ymd":"2022-04-30","week":"星期六","sunrise":"05:15","sunset":"19:07","aqi":43,"fx":"北风","fl":"3级","type":"多云","notice":"阴晴之间，谨防紫外线侵扰"},{"date":"01","high":"高温 34℃","low":"低温 19℃","ymd":"2022-05-01","week":"星期日","sunrise":"05:14","sunset":"19:08","aqi":18,"fx":"东南风","fl":"2级","type":"多云","notice":"阴晴之间，谨防紫外线侵扰"},{"date":"02","high":"高温 36℃","low":"低温 21℃","ymd":"2022-05-02","week":"星期一","sunrise":"05:12","sunset":"19:09","aqi":53,"fx":"南风","fl":"2级","type":"多云","notice":"阴晴之间，谨防紫外线侵扰"},{"date":"03","high":"高温 37℃","low":"低温 21℃","ymd":"2022-05-03","week":"星期二","sunrise":"05:11","sunset":"19:10","aqi":105,"fx":"东北风","fl":"2级","type":"小雨","notice":"雨虽小，注意保暖别感冒"}],"yesterday":{"date":"18","high":"高温 23℃","low":"低温 9℃","ymd":"2022-04-18","week":"星期一","sunrise":"05:31","sunset":"18:55","aqi":108,"fx":"东风","fl":"2级","type":"晴","notice":"愿你拥有比阳光明媚的心情"}}}</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="%E5%85%B6%E4%BB%96%E7%9A%84%E4%BA%A4%E4%BA%92%E5%BC%8F%E5%B7%A5%E5%85%B7">其他的交互式工具<a class="anchor-link" href="#%E5%85%B6%E4%BB%96%E7%9A%84%E4%BA%A4%E4%BA%92%E5%BC%8F%E5%B7%A5%E5%85%B7">¶</a>
</h3>
<ul>
<li>通过 dotnet-repl 也可在控制台上实现交互REPL。</li>
<li>通过 PowerShell 也能加载nuget，使用C#类。</li>
</ul>
</div>
</div>
</div>
</div>
    </div>
    <aside class="postpromonav"><nav><ul itemprop="keywords" class="tags">
<li><a class="tag p-category" href="../../categories/net/" rel="tag">.net</a></li>
            <li><a class="tag p-category" href="../../categories/aspnet/" rel="tag">aspnet</a></li>
            <li><a class="tag p-category" href="../../categories/c/" rel="tag">C#</a></li>
        </ul>
<ul class="pager hidden-print">
<li class="previous">
                <a href="../tiao-zhan-1xiao-shi-pythonbian-ma-ti-qu-he-suan-bao-gao-shu-ju/" rel="prev" title="挑战1小时python编码提取核酸报告数据">上一篇文章</a>
            </li>
            <li class="next">
                <a href="../tui-jian-wang-pan-tong-bu-pan-shu-ju-jia-mi-gong-ju-restic/" rel="next" title="推荐网盘同步盘数据加密工具restic">下一篇文章</a>
            </li>
        </ul></nav></aside><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML" integrity="sha384-3lJUsx1TJHt7BA4udB5KPnDrlkO8T6J6v/op7ui0BbCjvZ9WqV4Xm6DTP6kQ/iBH" crossorigin="anonymous"></script><script type="text/x-mathjax-config">
        MathJax.Hub.Config({tex2jax: {inlineMath: [['$latex ','$'], ['\\(','\\)']]}});
        </script></article><!--End of body content--><footer id="footer">
            Contents ©         <a href="https://vitamind3.gitee.io/">vitamind3</a> - Powered by         <a href="https://getnikola.com" rel="nofollow">Nikola</a>         
            
            
        </footer>
</div>
</div>


        <script src="../../assets/js/all-nocdn.js"></script><script>
    baguetteBox.run('div#content', {
        ignoreClass: 'islink',
        captions: function(element){var i=element.getElementsByTagName('img')[0];return i===undefined?'':i.alt;}});
    </script><script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?09027755f7478e129aec2cdd3acf9a4c";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>
</body>
</html>
